import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
    // 登录页面
    {
        path: '/views/Login',
        name: 'Login',
        component: () => import('../views/Login.vue'),
        meta: {
            loginCheck: false,
        },
    },
    {
        path: "/Home",
        name: "Home",
        component: () => import("../views/Home.vue"),
        meta: {
            loginCheck: true,
        },
    },
    {
        path: "/cityManage/index",
        name: "CityManage",
        component: () => import("../views/cityManage/index.vue"),
        meta: {
            loginCheck: true,
        },
    },
    {
        path: "/sanitation/index",
        name: "Sanitation",
        component: () => import("../views/sanitation/index.vue"),
        meta: {
            loginCheck: true,
        },
    },
    {
        path: "/green/index",
        name: "Green",
        component: () => import("../views/green/index.vue"),
        meta: {
            loginCheck: true,
        },
    },
    {
        path: "/municipal/index",
        name: "Municipal",
        component: () => import("../views/municipal/index.vue"),
        meta: {
            loginCheck: true,
        },
    },
    {
        path: "/heat/index",
        name: "Heat",
        component: () => import("../views/heat/index.vue"),
        meta: {
            loginCheck: true,
        },
    },
    {
        path: "/bright/index",
        name: "Bright",
        component: () => import("../views/bright/index.vue"),
        meta: {
            loginCheck: true,
        },
    },
    {
        path: "/garbageClass/index",
        name: "GarbageClass",
        component: () => import("../views/garbageClass/index.vue"),
        meta: {
            loginCheck: true,
        },
    },
];

const router = new VueRouter({
    /** 去掉项目中的url的# **/
    mode: process.env.NODE_ENV === 'development' ? 'history' : '',
    base: process.env.NODE_ENV === 'development' ? process.env.BASE_URL : '',
    routes
});

/**
 * Description:路由守卫（初始登录以及非法登录跳转到登录页）
 * Author:ybt
 * Date:2020/12/16
 */
router.beforeEach((to, from, next) => {
    let isLogin = JSON.parse(sessionStorage.getItem('loginInfo'));
    if(to.name){
        if(to.meta.loginCheck && !isLogin){
            next({
                name: 'Login',
            })
        }else{
            next();
        }
    }else{
        if(!isLogin){
            next({
                name: 'Login',
            })
        }else{
            next({
                name: 'Home',
            })
        }
    }
});

export default router;
